<?php
	$mysql_connect = mysql_connect('localhost', 'hr_mysql', 'hr_mysql');
	if ($mysql_connect)
	{
		mysql_select_db('hr_mysql', $mysql_connect);
		$loan_id = 0;
		$employee_id = 0;
		$loan_date = "";
		$loan_amount = 0;
		
		if(isset($_POST["btn_submit"]))
		{
			$loan_id = $_POST["txt_loan"];
			$employee_id = $_POST["ddl_employee"];
			$loan_date = $_POST["txt_date"];
			$loan_amount = $_POST["txt_amount"];
			
			if($loan_id == 0)
				$mysql_query = "insert into hs_hr_loan (date, employee_id, amount) values ('$loan_date', '$employee_id', '$loan_amount')";
			else
				$mysql_query = "update hs_hr_loan set date = '$loan_date', employee_id = '$employee_id', amount = '$loan_amount' where id = '$loan_id'";
			
			mysql_query($mysql_query);						
?>
			<script type="text/javascript">
				parent.$.fn.colorbox.close();
			</script>
<?php
			exit;
		}
		
		$mysql_query = "select * from hs_hr_employee";
		$mysql_result = mysql_query($mysql_query);
		if(!$mysql_result)		
			exit;
			
		$employee_list = array();
		while($mysql_row = mysql_fetch_array($mysql_result))
			$employee_list[] = $mysql_row;		
		
		if(isset($_GET["loan_id"]))
			$loan_id = $_GET["loan_id"];
		
		$mysql_query = "select * from hs_hr_loan where id = '$loan_id'";
		$mysql_result = mysql_query($mysql_query);
		if(!$mysql_result)
			exit;
			
		$loan = mysql_fetch_row($mysql_result);		
		if($loan != "")
		{
			$employee_id = $loan[2];
			$loan_date = $loan[1];
			$loan_amount = $loan[3];			
		}
	}
?>
<html>
	<head>
		<!-- JQuery -->
		<script src="./jsscripts/jquery/jquery.js" type="text/javascript"></script>

		<!-- DateTimePicker -->		
		<link href="./jsscripts/jquery/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
		<script src="./jsscripts/common.js" type="text/javascript"></script>
		<script src="./jsscripts/jquery/ui/jquery.ui.core.js" type="text/javascript"></script>
		<script src="./jsscripts/jquery/ui/jquery.ui.datepicker.js" type="text/javascript"></script>		
		<script src="./jsscripts/datepicker-base.js" type="text/javascript"></script>

		<!-- Colorbox -->		
		<link href="./jsscripts/colorbox/colorbox.css" rel="stylesheet" type="text/css" />
		<script src="./jsscripts/colorbox/jquery.colorbox.js" type="text/javascript"></script>
		
		<!-- Application -->
		<link href="./css/popup.css" rel="stylesheet" type="text/css" />
		
		<script type="text/javascript">
			//function Close
			//parent.$.fn.colorbox.close();
		</script>
	</head>
	<body>
		<div id="div_content">		
			<form id="frm_loan" method="post" action="./loan_handler.php">
				<input type="hidden" id="txt_loan" name="txt_loan" value="<?php echo $loan_id; ?>" />
				<table>
					<thead>
						<tr>
							<td colspan="3">
								Loan
							</td>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td colspan="3">&nbsp;</td>
						</tr>						
						<tr>
							<td class="td_label">Name</td>
							<td class="td_colon">:</td>
							<td class="td_detail">
								<select id="ddl_employee" name="ddl_employee" style="width: 310px;">									
<?php		
	foreach($employee_list as &$mysql_row)
	{
?>
									<option value="<?php echo $mysql_row['employee_id']; ?>" <?php if($employee_id == (int)$mysql_row['employee_id']) echo 'selected'; ?>><?php echo $mysql_row['emp_firstname']; ?></option>
<?php
	}
?>
								</select>
							</td>
						</tr>
						<tr>
							<td class="td_label">Date</td>
							<td class="td_colon">:</td>
							<td class="td_detail"><input type="text" id="txt_date" name="txt_date" class="date" value="<?php echo $loan_date ?>" /></td>
						</tr>
						<tr>
							<td class="td_label">Amount</td>
							<td class="td_colon">:</td>
							<td class="td_detail"><input type="text" class="input_number" id="txt_amount" name="txt_amount" value="<?php echo $loan_amount; ?>" /></td>
						</tr>
						<tr>
							<td colspan="3">&nbsp;</td>
						</tr>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="3">
								<input type="submit" id="btn_submit" name="btn_submit" onclick="parent.isUpdated=true;" />
								<input type="button" id="btn_cancel" name="btn_cancel" value="Cancel" onclick="parent.$.fn.colorbox.close(); parent.isUpdated=false;" />
							</td>
						</tr>
					</tfoot>
				</table>
			</form>			
		</div>		
	</body>
</html>
